package com.sgb.kjwl.viewmodel.self.collect;

import androidx.databinding.ObservableField;

import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.LogUtil;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.jicai.entity.JcCountEntity;
import com.sgb.kjwl.model.entity.self.SelfPickListEntity3;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.event.CollegeEvent;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.fragment.self.pick.SelfPickListFragment4;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SelfPickListFragModel4 extends BaseViewModel {
    public ObservableField<String> areaCode = new ObservableField<>("");
    public ObservableField<String> type = new ObservableField<>("");
    public ObservableField<String> status = new ObservableField<>("");
    public ObservableField<String> groupClass = new ObservableField<>("");
    public ObservableField<String> goodsName = new ObservableField<>("");
    private SelfPickListFragment4 fragment4;
    private SelfRepertory repertory;
    private int page = 1;
    private int size = 10;
    public int total;
    public boolean hasNext = false;//是否有下一页

    public SelfPickListFragModel4(SelfPickListFragment4 fragment4, SelfRepertory repertory) {
        this.fragment4 = fragment4;
        this.repertory = repertory;
    }

    //工程招采列表
    public void getList(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        repertory.getCollectList(page, size, status.get(), type.get(), groupClass.get(), goodsName.get())
                .subscribe(new Consumer<BaseEntity<ListResponse<List<SelfPickListEntity3>>>>() {
                    @Override
                    public void accept(BaseEntity<ListResponse<List<SelfPickListEntity3>>> listResponseBaseEntity) throws Exception {
                        if (listResponseBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            hasNext = !listResponseBaseEntity.getData().isLastPage();//最后一页取反判断是否有下一页
                            fragment4.showList(listResponseBaseEntity.getData().getList(), isLoadMore);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void delList(String no) {
        repertory.deleteCollectList(no)
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        if (baseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            EventBus.getDefault().post(CollegeEvent.make().setMessageTag(EventConstant.EVENT_COLLECT_SELF_LIST));
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void delList2(String no) {
        repertory.deleteCollectList2(no)
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        if (baseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            EventBus.getDefault().post(CollegeEvent.make().setMessageTag(EventConstant.EVENT_COLLECT_SELF_LIST));
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void getH5Url(String id, int status) {
        DialogHelper.showProgressDialog(fragment4.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("detail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id + "&type=" + status;
                    fragment4.showUrl(url);
                } else {
                    MToast.showToast(fragment4.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getMyJoinH5Url(String id, String status, int groupbuyType) {
        DialogHelper.showProgressDialog(fragment4.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("mine").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?toPage=join&no=" + id + "&status=" + status + "&groupbuyType=" + groupbuyType;
                    LogUtil.e("api","H5链接："+url);
                    fragment4.showUrl(url);
                } else {
                    MToast.showToast(fragment4.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * //刷新数量
     *
     * @param groupbuyTypes 我的集采订单类型    1 我发起的  2 我加盟的
     * @param groupBuyClass 集采分类            1商品集采 2 机械集采 3 设备集采
     */
    public void getMyGroupBuyListCount(int groupbuyTypes, int groupBuyClass, String searchWords) {
//        DialogHelper.showProgressDialog(fragment4.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        Map map = new HashMap();
        map.put("groupbuyTypes", groupbuyTypes);
        map.put("groupBuyClass", groupBuyClass);
        map.put("goodsName", searchWords);
        APIManager.getInstance().getPickAPI().myGroupBuyListCountForStatus(map).enqueue((new Callback<BaseEntity<List<JcCountEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<JcCountEntity>>> call, Response<BaseEntity<List<JcCountEntity>>> response) {
//                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment4.setTabNum(response.body().getData());
                } else {
                    MToast.showToast(fragment4.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<JcCountEntity>>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
            }
        }));
    }

    //停止集采
    public void stopJiCai(String id) {
        DialogHelper.showProgressDialog(fragment4.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getPickAPI().stopMyJicai(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    MToast.showToast(fragment4.getContext(), "停止集采成功");
                    fragment4.initData();
                } else {
                    MToast.showToast(fragment4.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //退出集采
    public void exitJiCai(String id) {
        DialogHelper.showProgressDialog(fragment4.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getPickAPI().exitMyJoin(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    MToast.showToast(fragment4.getContext(), "退出加盟成功");
                    fragment4.initData();
                } else {
                    MToast.showToast(fragment4.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


}